<template>
   <div class='greenhouse_create'>
    <el-page-header @back="goBack" content="添加数据"> </el-page-header>
    <el-form label-width="100px" size="small">
      <el-form-item label="名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-input  v-model="form.status"></el-input>
      </el-form-item>
      <el-form-item label="温度">
        <el-input v-model="form.temperature" ></el-input>
      </el-form-item>
            <el-form-item label="空气湿度">
        <el-input v-model="form.airHumidity" ></el-input>
      </el-form-item>
      <el-form-item label="土壤湿度">
        <el-input v-model="form.soilHumidity" ></el-input>
      </el-form-item>
            <el-form-item label="照片" >
          <!-- :file-list="falselist" -->
          <el-upload
          class="avatar-uploader"
          action="http://47.106.244.1:7788/file/upload"
          list-type="picture-card"
          :on-success="uploadSuccessHandler">
        </el-upload>

      </el-form-item>
      <el-form-item label="创建时间">
        <el-input v-model="form.createTime" ></el-input>
      </el-form-item>
      <el-form-item label="USER_ID">
        <el-input v-model="form.userId" ></el-input>
      </el-form-item>
      <el-form-item style="text-align:right">
        <el-button type="primary" @click="submitHandler">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { post } from '@/utils/request'
export default {
  //组件名称
  name:'demo',
  //组建参数，接受来自父组件的数据
  props:{},
  //组件状态值
  data(){
    return {
        imageUrl: '',
        files:[],
        // fileList:[],
        form:{},
      };
  },

  //计算属性
  computed:{},
  //侦听器
  watch:{},
  //组件方法
  methods:{

    // 上传成功的处理函数
    uploadSuccessHandler(resp){
      if(resp.status == 200){
        // 将fileID填充到form中，待会保存的时候提交给后台
        let id = resp.data.id;
        this.form = {
          ...this.form,
          id
        }
      } else {
        this.$message({type:"error",message:resp.message})
      }
    },


submitHandler(){
        // 1. 获取参数  this.form
      // 2. 将参数提交给后台
      let url = "http://121.196.106.124:8888/greenhouse_greenhouse/saveOrUpdate"
      post(url,this.form).then(resp => {
        // 提示用户录入完成
        this.$message({type:"success",message:resp.message})
        // 返回，展示
        this.goBack();
      })
    },
    goBack(){
      this.$router.back();
    }
  },
  
  created(){
    this.form = this.$route.query
  },
  }
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>